require "Task"
require "TaskCategory/RandomTask"
require "WeightPool"

QNW_TS_SearchAndDestroy = QNW_TS_Task:derive("QNW_TS_SearchAndDestroy")

local this = QNW_TS_SearchAndDestroy
this.name = getText("IGUI_QNW_TaskSystem_Task_SearchAndDestroy_Name")
this.desc = getText("IGUI_QNW_TaskSystem_Task_SearchAndDestroy_Desc")
this.taskCategory = QNW_TS_RandomTask

function QNW_TS_SearchAndDestroy:isComplete()
    return self:isTime()
end

function QNW_TS_SearchAndDestroy:complete()
    if QNW_TS_TaskWindow.instance then
        QNW_TS_TaskWindow.instance:destroy()
    end
    QNW_TS_Task.complete(self)
    local player = getPlayer()
    player:getModData().ZBnum = nil
    local direction = ZombRand(4)
    if direction == 0 then

        SearchAndDestorylocal(player, player:getX() + ZombRand(30, 45), player:getY(), 0, 3, 1)

    end
    if direction == 1 then

        SearchAndDestorylocal(player, player:getX() - ZombRand(30, 45), player:getY(), 0, 3, 1)

    end
    if direction == 2 then

        SearchAndDestorylocal(player, player:getX(), player:getY() + ZombRand(30, 45), 0, 3, 1)

    end
    if direction == 3 then

        SearchAndDestorylocal(player, player:getX(), player:getY() - ZombRand(30, 45), 0, 3, 1)

    end
    local warn = getText("IGUI_SearchAndDestroy_Warn")
    processGeneralMessage(warn)
end

function QNW_TS_SearchAndDestroy:isAbandon()
    return self:isTime()
end

function QNW_TS_SearchAndDestroy:getProgress()
    return ""
end

function QNW_TS_SearchAndDestroy:registerClient(_player)
    _player:Say(getText("IGUI_QNW_TaskSystem_Get", self.name))
end

function QNW_TS_SearchAndDestroy:save()
    local save = {}
    save.Type = self.Type
    save.uuid = self.uuid
    save.time = self.time
    save.rewards = {}
    for k, v in pairs(self.rewards) do
        table.insert(save.rewards, v:save())
    end
    return save
end

function QNW_TS_SearchAndDestroy:load(_table)
    self.uuid = _table.uuid
    self.time = _table.time
    if _table.rewards then
        self:loadReward(_table.rewards)
    end
end

function QNW_TS_SearchAndDestroy:new()
    local o = QNW_TS_Task:new()
    setmetatable(o, self)
    self.__index = self

    o.uuid = getRandomUUID()
    o.time = getGameTime():getCalender():getTimeInMillis() + 60 * 60 * 6000

    return o
end

function QNW_TS_SearchAndDestroy:initReward()
    QNW_TS_ClientCommand.randomReward(self, "premium", 2)
end

local rewardlist = {"Base.SuiPian", "Base.CreditCard", "Base.SuiPian"}
local rewardCountlist = {6, 3, 1}

function SearchAndDestorylocal(_player, _x, _y, _z, _count, _type)
    if not _count then
        _count = 1
    end
    local list = addZombiesInOutfit(_x, _y, _z, _count, nil, 50)
    if _type and _type == 1 then
        for i = 0, list:size() - 1 do
            local zombie = list:get(i)
            if i <= 1 then
                zombie:setHealth(30)
                zombie:setWalkType("sprint1")
            else
                zombie:setHealth(400)
            end
            zombie:getModData().playername = _player:getUsername()
            zombie:pathToCharacter(_player)
        end
    end
end

function SearchAndDestorycheckZombie(Zombie)
    local playerObj = getPlayer()
    if not playerObj then return end
    local playerName = playerObj:getUsername()
    if Zombie:hasModData() then
        if Zombie:getModData().playername == playerName then
            local num = ZombRand(1, 3)
            local Type = rewardlist[num]
            local Count = rewardCountlist[num]
            playerObj:getInventory():AddItems(Type, Count)
            local item = playerObj:getInventory():FindAndReturn(Type)
            local message = item:getName() .. "*" .. tostring(Count)
            playerObj:Say(message)
            Zombie:getModData().playername = nil
        end
    end
end
Events.OnZombieDead.Add(SearchAndDestorycheckZombie)
